// package/pages/groupFriend/groupFriend.js
const utils = require('../../../utils/util.js');
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxbarcode = require('../../../utils/wxbarcode/index.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    pop: false,
  },
  shouQr(e) {
    const that = this;
    const code = e.currentTarget.dataset.code;
    setTimeout(() => {
      wxbarcode.barcode("barcode", code, 680, 150);
      wxbarcode.qrcode("qrcode", code, {
        codeSize: 480,
        color: "#000000",
        bgcolor: "#ffffff",
      });
      that.setData({
        pop: true
      })
    }, 200)
  },
  cancel(e) {
    const that = this;
    wx.showModal({
      content: '是否取消该资格码？',
      success(res) {
        if (res.confirm) {
          let data = {
            token: wx.getStorageSync('token'),
            id: e.currentTarget.dataset.id
          }
          wx.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.cancelRecord, data).then(res => {
            wx.hideLoading();
            if (res.data.status == 1) {
              that.getData()
              wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {}
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toMyCrowdDetail(e) {
    const that = this;
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/package/pages/groupFriendDetail/groupFriendDetail?id=${id}`,
    })
  },
  remQr() {
    const that = this;
    that.setData({
      pop: false
    })
  },
  getData() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page,
      limit: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.recordList, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.paginate.data.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          that.setData({
            listData: res.data.result.paginate.data
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getData()
     
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    that.setData({
      isEnd: true,
      isWithout: true,
      page: 1,
    })
    wx.showNavigationBarLoading()
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page + 1,
      limit: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.recordList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.paginate.data.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.data.result.paginate.data],
            page: that.data.page + 1
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})